/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.startup;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.httpservice.HttpGetter;
import cn.ac.iie.di.wx.utils.RedisUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 * 用于补全信息
 *
 * @author Administrator
 */
public class CplStart {

    static Logger logger = Logger.getLogger(CplStart.class.getName());

//    public static void main(String[] args) throws ParseException {
//        try {
//            init();
//            //从起始日期开始列出这段日期
//            String start = ((String) RuntimeEnv.getParam(RuntimeEnv.STARTDATE));
//            String end = ((String) RuntimeEnv.getParam(RuntimeEnv.CLOSINGDATE));
//            logger.info(start + "---" + end);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            Date dBegin = sdf.parse(start);
//            Date dEnd = sdf.parse(end);
//            //初始化redis和一些参数
//            //发送给ict的接口
//            HttpGetter httpGetter = new HttpGetter();
//            //连接redis获取数据
//            String redisHosts = ((String) RuntimeEnv.getParam(RuntimeEnv.REDIS_HOSTS));
//            String masterName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_MASTER_NAME);
//            String dbName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_DB_NAME);
//            RedisUtil msgRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
//            String user = (String) RuntimeEnv.getParam(RuntimeEnv.USER);
//            String password = (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD);
//            //获取日期序列
//            List<Date> dates = listDate(dBegin, dEnd);
//            for (Date d : dates) {
//                String dateStr = sdf.format(d);
//                logger.info(dateStr);
//                //进行覆盖
//                reWrite(msgRedisUtil, masterName, user, password, httpGetter);
//            }
//            logger.info(dates.size());
//        } catch (Exception ex) {
//            java.util.logging.Logger.getLogger(CplStart.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//    }
    /*
     获取两个日期之间的所有日期序列，包括起始日期但不包括截止日期
     */
    public static void main(String[] args) {
        try {
            init();
            HttpGetter httpGetter = new HttpGetter();
//            reWriteByProperties("1432485", "1432464", "151948621", "31367469", "20170419", "xgs", "xgs.123456", httpGetter);
//            reWriteByProperties("1458353", "1458341", "98936582", "20317540", "20170420", "xgs", "xgs.123456", httpGetter);
//            reWriteByProperties("1810530", "1810496", "117414374", "24237081", "20170421", "xgs", "xgs.123456", httpGetter);
//            reWriteByProperties("1335721", "1335697", "112928926", "23950902", "20170422", "xgs", "xgs.123456", httpGetter);
            logger.info("date:"+(String) RuntimeEnv.getParam(RuntimeEnv.DATE));
            reWriteByProperties(
                    (String) RuntimeEnv.getParam(RuntimeEnv.MASSALLDATA), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.MASSSUCDATA), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.SINGLEALLDATA), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.SINGLESUCDATA), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.DATE), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.USER), 
                    (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD), httpGetter);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(CplStart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static List<Date> listDate(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        //lDate.add(dBegin);  
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间    
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间    
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后    
        while (dEnd.after(calBegin.getTime())) {
            lDate.add(calBegin.getTime());
            calBegin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return lDate;
    }
    /*
     根据日期进行数据覆盖
     */

    public static void reWrite(RedisUtil msgRedisUtil, String yesterdayDate, String user, String password, HttpGetter httpGetter) {
        String massAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSALL) + yesterdayDate);//模拟
        String massSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSSUC) + yesterdayDate);
        String singleAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLEALL) + yesterdayDate);
        String singleSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLESUC) + yesterdayDate);
        logger.info("massAll=" + massAll + "massSuc=" + massSuc + "singleAll=" + singleAll + "singleSuc=" + singleSuc);

        // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
//            String evaluate = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE)+yesterdayDate);
        if (massAll != null && massSuc != null && singleAll != null && singleSuc != null) {
            String msgrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                    + "&massAll=" + massAll + "&massSuc=" + massSuc + "&singleAll=" + singleAll + "&singleSuc=" + singleSuc;
            String msg_url = (String) RuntimeEnv.getParam(RuntimeEnv.MSG_URL) + "?";
            httpGetter.doGet(msg_url + msgrequest);
        } else {
            logger.warn("can not get msg datas from redis");
        }
    }

    public static void reWriteByProperties(String massAll, String massSuc, String singleAll, String singleSuc, String yesterdayDate, String user, String password, HttpGetter httpGetter) {
        logger.info("massAll=" + massAll + "massSuc=" + massSuc + "singleAll=" + singleAll + "singleSuc=" + singleSuc);

        // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
//            String evaluate = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE)+yesterdayDate);
        if (massAll != null && massSuc != null && singleAll != null && singleSuc != null) {
            String msgrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                    + "&massAll=" + massAll + "&massSuc=" + massSuc + "&singleAll=" + singleAll + "&singleSuc=" + singleSuc;
            String msg_url = (String) RuntimeEnv.getParam(RuntimeEnv.MSG_URL) + "?";
            httpGetter.doGetByDate(msg_url + msgrequest,yesterdayDate);
        } else {
            logger.warn("can not get msg datas from redis");
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "serverport.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            throw new Exception("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
        } catch (Exception ex) {
            throw new Exception("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }
}
